<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
//        $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');

        if (is_null($_SESSION["USER"])) {
            echo '<script> alert("请先登录")</script>';
            $this->redirect("loginshow");
        }
        $this->display('Web/index');
    }

    public function exitlogin(){
        $_SESSION['USER'] = null;
        $this->display("/Web/login");
    }

    public function loginshow(){
        $this->display("Web/login");
    }
    public function topup(){
        if (is_null($_SESSION["USER"])) {
            echo '<script> alert("请先登录")</script>';
            $this->redirect("Index/login");
        }

        $userid = $_SESSION['USER']['id'];
        $carid = $_POST['timecard'];
        if(!$carid)
        {
            echo '<script> alert("点卡号有误")</script>';
            $this->redirect("Index/index");
        }

        $card = M('timecard')->where('user = -1 and carid =  "'.$carid.'"')->find();


        if($card){

            $card['user'] = $userid;
            M('timecard')->save($card);

            $user = M('userinfo')->where('id= "'. $userid.'"')->find();
            $user['time'] =strtotime('+'.$card['time'].' day', $user['time']);
            if($_POST['signature']) {
                $user['signature'] = $_POST['signature'];
            }
            $user['type']=2;
            M('userinfo')->save($user);
            $_SESSION['USER']['time'] = $user['time'];
            echo '<script> alert("充值成功")</script>';
            $this->redirect("/Index/index");
        }else{
            echo '<script> alert("点卡号有误")</script>';
            $this->redirect("/Index/index");
        }
    }

    public function topupm(){
        $userid =$_POST['userid'];
        $carid = $_POST['timecard'];
        $check = $_POST['check'];


        if($check == md5(($userid.'androidhello'))){

            $card = M('timecard')->where('user = -1 and carid =  "'.$carid.'"')->find();
            if($card){
                $card['user'] = $userid;
                M('timecard')->save($card);
                $user = M('userinfo')->where('id= "'. $userid.'"')->find();
                $user['time'] =strtotime('+'.$card['time'].' day', $user['time']);
//                $user['signature'] = $_POST['signature'];
                $user['type']=2;
                M('userinfo')->save($user);

                $data['code'] = 200;
                $data['msg'] = '充值成功';
                $data['data'] = date('Y-m-d H:i',$user['time']);
//                $data['data'] = '222';
                echo json_encode($data);

            }else{

                $data['code'] = 202;
                $data['msg'] = '点卡号错误';
                $data['data'] = '点卡号错误';
                echo json_encode($data);
            }
        }else {
            $data['code'] = 201;
            $data['msg'] = '校验错误';
            $data['data'] = '校验错误';
            echo json_encode($data);
        }

    }



    public function login(){
        $usernaem = $_POST['username'];
        $password = $_POST['password'];

        $result = M("userinfo")->where("username='" . $usernaem . "'")->find();
        if ($result) {
            if ($result['password'] == md5($password))
            {
                $_SESSION['USER'] = $result;
                $this->redirect('index/index');
            }else{
                echo '<script> alert("账号或密码错误")</script>';
                $this->display("/Web/login");
            }
        }else{
            echo '<script> alert("账号或密码错误")</script>';
            $this->display("/Web/login");
        }
    }

    public function checking()
    {
        $usernaem = $_POST['username'];
        $password = $_POST['password'];
        $signature = $_POST['signature'];

        $version = $_POST['version'];

        $rid = $_POST['rid'];
        $result = M("userinfo")->where("username='" . $usernaem . "'")->find();
        if ($result) {
            if ($result['password'] == $password && (intval($result['time']) >= time())) {

                if( !is_null($version)||( is_null($version)&&$result['signature'] == $signature)) {

                    if( !is_null($version)){
                        $result['signature'] = $signature;
                    }

                    $result['rid'] = $rid;
                    M('userinfo')->save($result);
                    $data['msg'] = '验证通过';
                    $data['data'] = date('Y-m-d H:i', $result['time']) . 'UID:' . $result['id'];
                    $data['code'] = 200;
                    echo json_encode($data);
                }else{
                    $data['msg'] = '密码错误或者过期';
                    $data['data'] = '密码错误或者过期';
                    $data['code'] = 201;
                    echo json_encode($data);
                }
            } else {
                $data['msg'] = '密码错误或者过期'.intval($result['time']);
                $data['data'] = '密码错误或者过期'.time();
                $data['code'] = 201;
                echo json_encode($data);
            }
        } else {
            $data['msg'] = '没有账号.请联系Q577015979开通';
            $data['data'] = '没有账号';
            $data['code'] = 202;
            echo json_encode($data);
        }
    }


    public function timecheck(){
        $userid =  $_POST['userid'];
        $signature = $_POST['signature'];
        $result  = M('userinfo')->where('id= "'. $userid.'"')->find();
        if ($result) {
           if($result['signature'] == $signature && (intval($result['time']) >= time())){
               $data['msg'] = '验证通过';
               $data['data'] = 'hello';
               $data['code'] = 200;
               echo json_encode($data);
           }else{
               $data['msg'] = '过期或特征码不正确';
               $data['data'] = '过期或特征码不正确';
               $data['code'] = 201;
               echo json_encode($data);
           }
        }else{
            $data['msg'] = '过期或特征码不正确';
            $data['data'] = '过期或特征码不正确';
            $data['code'] = 201;
            echo json_encode($data);
        }

    }

    public function update()
    {
        $sys = M('sys')->find();
        $v['version'] = $sys['version'];
        $v['address'] = $sys['address'];
        $v['type'] = $sys['type'];
        $data['code'] = 200;
        $data['msg'] = '';
        $data['data'] = $v;
        echo json_encode($data);
    }

    public function register(){
        $this->display("/Web/register");
    }


    public function registered(){
        $isPayUser = false;
        $date['username'] = $_POST['username'];
        if (M('userinfo')->where('username= "'. $date['username'].'"')->find())
        {
            echo '<script> alert("注册失败")</script>';
            $this->redirect("Index/register");
        }

        $date['password'] = md5($_POST['password']);
        $date['signature'] = $_POST['signature'];


        $carid =  $date['carid'];
        if($carid){

            $card = M('timecard')->where('user = -1 and carid =  "'.$carid.'"')->find();


            if($card){
                $isPayUser = true;
                $date['time'] =strtotime('+'.$card['time'].' day');
                $date['type']=2;

            }else{
                $date['time']=strtotime('+9 minute');
                $date['type']=1;
            }

        }else{
            $date['time']=strtotime('+9 minute');
            $date['type']=1;
        }


        $date['rid']='0';
        $date['adminid']=2;
        $userid = M('userinfo')->add($date);

        if($isPayUser){
            $card['user'] = $userid;
            M('timecard')->save($card);
        }

        echo '<script> alert("注册成功")</script>';
        $this->redirect("Index/login");
    }



    /**
     * 检查用户名是否存在
     * @param $param
     */
    public function usercheck($param)
    {

        if(M('userinfo')->where("username='".$param."'")->find())
        {
            echo "用户名已经存在";
        }else{
            echo "y";
        }
    }


}
